package com.zxs.controller;

import com.zxs.annotation.RequiresPermissions;
import com.zxs.client.UserClient;
import com.zxs.entity.User;
import com.zxs.service.IBrandService;
import com.zxs.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;


@RestController
@RequestMapping("/brand")
@Slf4j
public class BrandController {
	@Autowired
	private IBrandService brandService;
	@Autowired
	private UserClient client;
	@Autowired
	private LoadBalancerClient loadBalancerClient;
	@Autowired
	private RestTemplate restTemplate;
	//1. 查询品牌列表
	@GetMapping("/list")
	@RequiresPermissions("manager:brand:list")
	public R list(){
		// ServiceInstance instance = loadBalancerClient.choose("sprinfcloud-user");
		// String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/user/list";
		// log.info("url:{}",url);
		// R<List<User>> body = restTemplate.getForEntity(url, R.class).getBody();
		//
		// log.info("用户列表：{}",body);

		return R.ok(brandService.list());
	}

	//2. 查询用户列表
	@GetMapping("/user/list")
	public R<List<User>> userList(){
		return client.list();
	}

	//3. 查询用户端口
	@GetMapping("/user/getPort")
	public R getPort(){
		return client.getPort();
	}
}
